var qf = {
    dualselects: {}
};

qf.moveOptions = function(srcId, destId, keepSorted) {
    var src  = document.getElementById(srcId);
    var dest = document.getElementById(destId);

    for (var i = src.options.length - 1; i >= 0; i--) {
        if (src.options[i].selected) {
            var option = src.options[i];
            src.remove(i);
            option.selected = false;
            qf.addOption(dest, option, keepSorted);
        }
    }
};

qf.addOption = function(box, option, keepSorted) {
    /*@cc_on
    return qf.addOptionIE(box, option, keepSorted);
    @*/
    return qf.addOptionDOM(box, option, keepSorted);
};

qf.addOptionIE = function(box, option, keepSorted) {
    if (box.options.length <= 0 || (keepSorted && option.text < box.options[0].text))  {
        box.add(option, 0);

    } else if (!keepSorted || (option.text > box.options[box.options.length - 1].text)) {
        box.add(option);

    } else {
        for (var i = box.options.length; i >= 0; i--) {
            if (option.text >= box.options[i-1].text) {
                box.add(option, i);
                break;
            }
        }
    }
    return true;
};

qf.addOptionDOM = function(box, option, keepSorted) {
    if (!keepSorted || 0 == box.options.length ||
        option.text > box.options[box.options.length-1].text
    ) {
        box.add(option, null);

    } else if (option.text < box.options[0].text) {
        box.add(option, box.options[0]);

    } else {
        for (var i = box.options.length - 1; i >= 0; i--) {
            if (option.text >= box.options[i].text) {
                box.add(option, box.options[i + 1]);
                break;
            }
        }
    }
    return true;
};

qf.selectAll = function(event) {
    for (var i in qf.dualselects) {
        var select = document.getElementById(i);
        for (var j = 0; j < select.options.length; j++) {
            select.options[j].selected = true;
        }
    }
};

qf.addOnSubmitHandler = function() {
    var forms = document.getElementsByTagName('form');
    for (var i = 0; i < forms.length; i++) {
        if (forms[i].addEventListener) {
            forms[i].addEventListener('submit', qf.selectAll, false);
        } else if (forms[i].attachEvent) {
            forms[i].attachEvent('onsubmit', qf.selectAll);
        }
    }
};

window.onload = qf.addOnSubmitHandler;